package com.changcheng.service.Imp;

import com.changcheng.aspect.ServiceOperation;
import com.changcheng.mapper.UserMapper;
import com.changcheng.pojo.Do.PageInfo;
import com.changcheng.pojo.Product;
import com.changcheng.pojo.Role;
import com.changcheng.pojo.User;
import com.changcheng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xuan
 * @create 2021--04--12
 */
@Service
public class UserServiceImp implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public int selectItemsNumber() {
        return userMapper.selectItemsNumber();
    }
    @Override
    public List<User> findAllUser(PageInfo pageInfo) {
        return userMapper.findAllUser(pageInfo);
    }


    //@Override
    //public String findPermissionByUserId(int id) {
    //   return userMapper.findPermissionByUserId(id);
    //}
    public List<String> findPermissionByUserId(int id) {
        return userMapper.findPermissionByUserId(id);
    }

    @Override
    @Transactional
    @ServiceOperation("添加用户")
    public void addUser(User user,int roleId) {
        userMapper.addUser(user, roleId);
        int userId=userMapper.findIdByUsername(user.getUsername());
        userMapper.addUserRole(userId,roleId);
    }

    @Override
    public List<Role> findAllRole() {
        return userMapper.findAllRole();
    }

    @Override
    public int findIdByUsername(String username) {
        return userMapper.findIdByUsername(username);
    }

    @Override
    public void addUserRole(int userId, int roleId) {
        userMapper.addUserRole(userId,roleId);
    }

    @Override
    @ServiceOperation("更改密码")
    public Boolean updatePassword(String password,String username) {
        if(password!=null&&username!=null){
            userMapper.updatePassword(password,username);
            return true;
        }
        else {
            return false;
        }

    }

   /* @Override
    public List<String> findAllUserAndRole() {
        return userMapper.findAllUserAndRole();
    }*/

    @Override
    @Transactional
    @ServiceOperation("删除用户")
    public Boolean deleteUser(int id) {
        userMapper.deleteUser_Role(id);
        userMapper.deleteUser(id);
        return true;
    }

    @Override
    public List<String> findCurrentUserRole(int role) {
        return userMapper.findCurrentUserRole(role);
    }


}
